<?php
$s="123计算机";
echo strlen($s),"\n";
echo mb_strlen($s),"\n";
var_dump(strcmp("hello","hi"));
$a='计算机123'; //E8 AE A1 E7 AE 97 E6 9C BA 31 32 33
echo bin2hex($a),"\n"; //e8aea1e7ae97e69cba313233
echo hex2bin('e8aea1e7ae97e69cba313233'),"\n";
//chunk_split把字符串用指定的字符分隔开来
echo chunk_split("123456", 2, ":"),"\n"; //12:34:56:
echo chunk_split("e8aea1e7ae97e69cba313233", 2, " "),"\n"; //e8 ae a1 e7 ae 97 e6 9c ba 31 32 33
echo chunk_split("e8aea1e7ae97e69cba313233", 2, "_"),"\n"; //e8_ae_a1_e7_ae_97_e6_9c_ba_31_32_33
//str_replace字符串替换
$ss="How old are you?";
echo str_replace('o', '**', $ss),"\n";//H**w **ld are y**u?
//从字符串中，不管找到o还是r，统统替换成*
echo str_replace(['o','r'], '*', $ss),"\n";//H*w *ld a*e y*u?
//字符串中，找到o替换成O，找到r替换成R
echo str_replace(['o','r'],['O','R'],$ss),"\n";//How Old aRe you?
//两个字符串，都按上面的规则处理
print_r(str_replace(['o','r'],['O','R'],[$ss,'Hero']));
// Array
// (
//     [0] => How Old aRe you?
//     [1] => HeRO
// )
echo str_replace(' ', '', $ss),"\n"; //HowOldareyou?
//str_repeat多次重复字符串

echo str_repeat("Ok", 3), "\n"; //OkOkOk
echo str_repeat("Ok", 0), "\n"; //[]
//join将数组元素拼接成字符串
echo join(["Hi", "Zhangsan"]), "\n"; //HiZhangsan
echo join(", ", [123,456,789]), "\n"; //123, 456, 789
//strtolower、strtoupper转换成小写、大写
echo strtolower("How Are You.\n"); //how are you.
echo strtoupper("I'm fine, Thank you.\n"); //I'M FINE, THANK YOU.
echo strtoupper("e8_ae_a1_e7_ae_97\n"); //E8_AE_A1_E7_AE_97
//printf显示格式化字符串
$a=32.856;
printf("[%f] [%1$.1f] [%1$6.1f] [%1$-6.1f]\n", $a); //[32.856000] [32.9]  [ 32.9] [32.9  ]

$a=32.856;
printf("[%f] [%1$.1f] [%1$6.1f] [%1$-6.1f]\n", $a); //[32.856000] [32.9]  [ 32.9] [32.9  ]
$s="Hello World!";
printf("[%s] [%1$.5s] [%1$8.5s] [%1$-8.5s]\n", $s); //[Hello World!] [Hello] [  Hello] [Hello   ]
$s="汉字";
printf("[%s] [%1$.3s] [%1$6.3s] [%1$-8.3s]\n", $s); //[汉字] [汉] [  汉] [汉-----]
//substr取子串
echo substr("Hello", 0, 3),"\n"; //Hel
echo substr("Hello", 2),"\n"; //llo
//-3表示字符串后边的3个字符不要了
echo substr("Hello", 0, -3),"\n"; //He

echo substr("Hello", -4, -2),"\n"; //He

//str_starts_with、str_ends_with 以子串开始或结束
var_dump(str_starts_with("Hello","ll")); //bool(false)
var_dump(str_starts_with("Hello","he")); //bool(false)
var_dump(str_starts_with("Hello","He")); //bool(true)
var_dump(str_ends_with("Hello","llo")); //bool(true)

//将数组中的每项数据及其对应的 16 进制串，两端对齐显示在指定的宽度内，若宽度不足，则两项数据直接显示。
$a=['张三丰','里  斯','阿 kit','papi 酱','Jackson'];
outFormat(35,$a); //利用函数处理数组数据, 输出内容在35个英文字符宽度内

//完善函数功能，将数组中的每项数据及其16进制串，两端对齐显示在指定的宽度内
//数组中的每一项数据中包含的空格要删除掉
//16进制串中每个字节占两个16进制位，英文字符采用大写，各字节间以_分隔
//本文件存储采用utf-8编码
function outFormat(int $width, ?array $a):void {
    if (is_null($a)) return;
    foreach ($a as $item) {
        $name = str_replace(' ', '', $item);
        $you = bin2hex($name);
        $you = strtoupper($you);
        $you = chunk_split($you, 2, '_');

        $you = substr($you, 0, -1);
        $rightch = strlen($you);
        $namech = strlen(iconv('utf-8', 'GBK', $name));
        $spaceLength = $width - $namech - $rightch;
        if ($spaceLength < 5) $spaceLength = 10;
        echo $name, str_repeat(' ', $spaceLength), $you, "\n";
    }

}
/* 输出内容如下：
张三丰   E5_BC_A0_E4_B8_89_E4_B8_B0
里斯              E9_87_8C_E6_96_AF
阿kit             E9_98_BF_6B_69_74
papi酱         70_61_70_69_E9_85_B1
Jackson        4A_61_63_6B_73_6F_6E
*/